#include <gtest/gtest.h>
#include "Util/base64.h"
#include <cstring>

// 测试 av_base64_encode 函数
TEST(Base64Test, AvBase64Encode) {
    const uint8_t input[] = "abc:def";
    const int input_size = strlen(reinterpret_cast<const char*>(input));
    const char* expected = "YWJjOmRlZg==";
    int out_size = AV_BASE64_SIZE(input_size);
    char output[out_size];

    char* result = av_base64_encode(output, out_size, input, input_size);
    ASSERT_NE(result, nullptr);
    EXPECT_STREQ(result, expected);
}

// 测试 av_base64_decode 函数
TEST(Base64Test, AvBase64Decode) {
    const char* input = "YWJjOmRlZg==";
    const uint8_t expected[] = "abc:def";
    const int expected_size = strlen(reinterpret_cast<const char*>(expected));
    uint8_t output[expected_size + 1];

    int result_size = av_base64_decode(output, input, expected_size + 1);
    ASSERT_GT(result_size, 0);
    output[result_size] = '\0';
    EXPECT_STREQ(reinterpret_cast<const char*>(output), reinterpret_cast<const char*>(expected));
}

// 测试 encodeBase64 函数
TEST(Base64Test, EncodeBase64) {
    std::string input = "abc:def";
    std::string expected = "YWJjOmRlZg==";

    std::string result = encodeBase64(input);
    EXPECT_EQ(result, expected);
}

// 测试 decodeBase64 函数
TEST(Base64Test, DecodeBase64) {
    std::string input = "YWJjOmRlZg==";
    std::string expected = "abc:def";

    std::string result = decodeBase64(input);
    EXPECT_EQ(result, expected);
}

// 测试空字符串编码
TEST(Base64Test, EncodeEmptyString) {
    std::string input = "";
    std::string expected = "";

    std::string result = encodeBase64(input);
    EXPECT_EQ(result, expected);
}

// 测试空字符串解码
TEST(Base64Test, DecodeEmptyString) {
    std::string input = "";
    std::string expected = "";

    std::string result = decodeBase64(input);
    EXPECT_EQ(result, expected);
}

// 测试非法 base64 字符串解码
TEST(Base64Test, DecodeInvalidBase64) {
    std::string input = "!@#$%^&*";
    std::string expected = "";

    std::string result = decodeBase64(input);
    EXPECT_EQ(result, expected);
}

int main(int argc, char **argv) {
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}